package com.damon.g2048.utils;

import java.io.File;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;

import org.apache.http.conn.util.InetAddressUtils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
import android.os.StatFs;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.widget.Toast;

public class EbtUtils {
	/** 没有网络 */
	public static final int APN_TYPE_NONE = -1;
	/** Mobile data connection */
	public static final int APN_TYPE_MOBILE = 0;
	/** WIFI data connection */
	public static final int APN_TYPE_WIFI = 1;

	public EbtUtils() {

	}

	/**
	 * 检测sd卡是否存在
	 * 
	 * @return
	 */
	public static boolean checkSdCard() {
		if (android.os.Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED)) {
			return true;
		}
		return false;
	}

	/**
	 * 获取当前的网络状态
	 * 
	 * @param context
	 * @return -1:没有网络 1:wifi 2:wap 3: net网络
	 */
	public static int getAPNType(Context context) {
		ConnectivityManager connMgr = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
		if (networkInfo == null) {
			return APN_TYPE_NONE;
		}
		switch (networkInfo.getType()) {
		case ConnectivityManager.TYPE_MOBILE:
			return APN_TYPE_MOBILE;
		case ConnectivityManager.TYPE_WIFI:
			return APN_TYPE_WIFI;
		default:
			return APN_TYPE_NONE;
		}
	}

	/**
	 * 网络可用
	 * 
	 * @param context
	 * @return
	 */
	public static boolean isNetworkEnabled(Context context) {
		return APN_TYPE_NONE != getAPNType(context);
	}

	/**
	 * 短Toast
	 * 
	 * @param context
	 * @param text
	 */
	public static void smallToast(Context context, String text) {
		Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
	}

	/**
	 * 短Toast
	 * 
	 * @param context
	 * @param text
	 */
	public static void smallCenterToast(Context context, String text) {
		Toast to = Toast.makeText(context, text, Toast.LENGTH_SHORT);
		to.setGravity(Gravity.CENTER, 0, 0);
		to.show();
	}

	/**
	 * 长Toast
	 * 
	 * @param context
	 * @param text
	 */
	public static void bigToast(Context context, String text) {
		Toast.makeText(context, text, Toast.LENGTH_LONG).show();
	}

	/**
	 * 根据类名获得tag
	 */
	public static String getTag(Class clazz) {
		if (null != clazz) {
			return clazz.getName();
		}
		return "";
	}

	/**
	 * 获取DisplayMetrics对象
	 * 
	 * @param v
	 * @return
	 */
	public static DisplayMetrics getDisplay(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		dm = context.getResources().getDisplayMetrics();
		return dm;
	}

	/**
	 * 获取屏幕宽度
	 * 
	 * @param v
	 * @return
	 */
	public static int getScreenWidth(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		dm = getDisplay(context);
		return dm.widthPixels;
	}

	/**
	 * 获取屏幕高度
	 * 
	 * @param v
	 * @return
	 */
	public static int getScreenHeight(Context context) {
		DisplayMetrics dm = new DisplayMetrics();
		dm = getDisplay(context);
		return dm.heightPixels;
	}

	/**
	 * 获得本机ip地址方式1
	 * 
	 * @return
	 */
	public String getLocalIpAddress() {
		try {
			for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en
					.hasMoreElements();) {
				NetworkInterface intf = en.nextElement();
				for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr
						.hasMoreElements();) {
					InetAddress inetAddress = enumIpAddr.nextElement();
					if (!inetAddress.isLoopbackAddress()) {
						return inetAddress.getHostAddress().toString();
					}
				}
			}
		} catch (SocketException ex) {
			Log.e("WifiPreference IpAddress", ex.toString());
		}
		return null;
	}

	/**
	 * 获得本机Ip地址方式2
	 * 
	 * @param useIpv4
	 * @return
	 */
	public static String getLoacalIpAddress(boolean useIpv4) {
		try {
			List<NetworkInterface> interfaces = Collections.list(NetworkInterface
					.getNetworkInterfaces());
			List<InetAddress> addrs = null;
			for (NetworkInterface intf : interfaces) {
				addrs = Collections.list(intf.getInetAddresses());
				for (InetAddress addr : addrs) {
					// 回环地址127.8.
					if (!addr.isLoopbackAddress()) {
						String sAddr = addr.getHostAddress().toUpperCase();
						boolean isIpv4 = InetAddressUtils.isIPv4Address(sAddr);
						if (useIpv4) {
							if (isIpv4) {
								return sAddr;
							}
						} else {
							if (!isIpv4) {
								int delim = sAddr.indexOf("%");// ipv6端口前面的前缀
								return delim < 0 ? sAddr : sAddr.substring(0, delim);
							}
						}
					}
				}
			}
		} catch (Exception e) {

		}
		return "";
	}

	/**
	 * 获得本机网卡地址
	 * 
	 * @return
	 */
	public String getLocalMacAddress(Context context) {
		WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
		WifiInfo info = wifi.getConnectionInfo();
		return info.getMacAddress();
	}

	/**
	 * 获得当前目录下的可用空间
	 * 
	 * @param path
	 *            目标目录
	 * @return
	 */
	public static long getUsableSpace(File path) {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
			return path.getUsableSpace();
		}
		final StatFs stats = new StatFs(path.getPath());
		return (long) stats.getBlockSize() * (long) stats.getAvailableBlocks();
	}

	/**
	 * 判断是否外置存储存在
	 * 
	 * @return
	 */
	public static boolean isExternalStorageRemovable() {
		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
			return Environment.isExternalStorageRemovable();
		}
		return true;
	}


	/**
	 * 动态调整listview高度
	 * 
	 * @param listView
	 */
	public static void setListViewHeightBasedOnChildren(ListView listView) {
		ListAdapter listAdapter = listView.getAdapter();
		if (listAdapter == null) {
			// pre-condition
			return;
		}

		int totalHeight = 0;
		for (int i = 0; i < listAdapter.getCount(); i++) {
			View listItem = listAdapter.getView(i, null, listView);
			listItem.measure(0, 0);
			totalHeight += listItem.getMeasuredHeight();
		}

		ViewGroup.LayoutParams params = listView.getLayoutParams();
		params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
		listView.setLayoutParams(params);
	}

}
